package com.revzone.demo001.controller;


import com.revzone.demo001.entity.Result;
import com.revzone.demo001.entity.Role;
import com.revzone.demo001.entity.RoleDTO;
import com.revzone.demo001.mapper.RoleMapper_old;
import com.revzone.demo001.service.RoleService_old;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 *  前端控制器
 * @author lsa
 * @since 2023-03-27
 */
@RestController
@Api(tags = "角色role管理", description = "用于角色role管理的相关API")
@RequestMapping("/role")
public class RoleController_old {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    RoleService_old roleService;
    @Autowired
    RoleMapper_old roleMapper;

    @RequiresRoles(logical = Logical.OR, value = {"user","admin"})
    @PostMapping(value="/list", produces = "application/json;charset=UTF-8")
    public Result getRoleList(@RequestBody(required = false) Role rolechild) {
        logger.info("欢迎来到角色查询接口!");
//        if (role == null) role = new Role();
////        return roleService.getRoleList(role);
//        return Result.success(roleService.list());

        List<Role> roles = roleService.list(); // 获取所有基本角色信息

        return Result.success(roles.stream().map(role -> {
            List<String> permissions = roleMapper.findpermissioncodeByName(role.getRole_name()); // 调用现有的接口
            return new RoleDTO(
                    role.getId(),
                    role.getRole_name(),
                    role.getNick_name(),
                    role.getMenu_list(),
                    role.getCreate_date(),
                    role.getUpdate_date(),
                    role.getStatus(),
                    role.getRemarks(),
                    permissions
            );
        }).collect(Collectors.toList()));
    }


    @PostMapping(value="/add", produces = "application/json;charset=UTF-8")
    public Result setRoleList(@RequestBody(required = false) RoleDTO roleDTO) {
        logger.info("欢迎来到角色新增接口!");
        return roleService.setUserList(roleDTO);
    }

    @PostMapping(value="/update", produces = "application/json;charset=UTF-8")
    public Result updateRoleList(@RequestBody(required = false) RoleDTO roleDTO) {
        logger.info("欢迎来到角色修改接口!");
//        if (role == null) role = new Role();
        //添加权限
        return roleService.updateUserList(roleDTO);
    }

    @GetMapping(value="/delete", produces = "application/json;charset=UTF-8")
    public Result delRoleList(@RequestParam(required = false,defaultValue = "0",value = "id") Integer id) {
        logger.info("欢迎来到角色删除接口!");
        return roleService.delUserList(id);
    }

}

